<template>
  <el-card class="platform-card" shadow="hover">
    <template #header>
      <div class="card-header">
        <span class="platform-name" :style="{ color: platform.color }">
          {{ platform.name }}热搜榜
        </span>
        <el-tag type="info">实时</el-tag>
      </div>
    </template>
    
    <div class="hot-search-list">
      <div 
        v-for="(item, index) in hotSearches" 
        :key="item.id"
        class="hot-search-item"
        @click="$emit('item-click', item)"
      >
        <div class="ranking" :class="getRankingClass(index)">{{ index + 1 }}</div>
        <div class="content">
          <div class="title">{{ item.title }}</div>
          <div class="hot-value">
            <svg class="icon" viewBox="0 0 1024 1024" width="14" height="14">
              <path d="M858.9 689L530.5 308.2c-9.4-10.9-27.5-10.9-37 0L165.1 689c-12.2 14.2-1.2 35 18.5 35h656.8c19.7 0 30.7-20.8 18.5-35z"></path>
            </svg>
            {{ formatHotValue(item.hotValue) }}
          </div>
        </div>
        
        <!-- 添加分类标签 -->
        <div class="category-tag">
          <el-tag size="mini" :type="getTagType(item.category)">
            {{ getCategoryName(item.category) }}
          </el-tag>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { defineProps, computed } from 'vue'
import { useNewsStore } from '@/stores/news'

const props = defineProps({
  platform: {
    type: Object,
    required: true
  },
  hotSearches: {
    type: Array,
    default: () => []
  }
})

defineEmits(['item-click'])

const newsStore = useNewsStore()
const { categories } = storeToRefs(newsStore)

const formatHotValue = (value) => {
  if (value >= 1000000) {
    return (value / 1000000).toFixed(1) + 'm'
  }
  if (value >= 1000) {
    return (value / 1000).toFixed(1) + 'k'
  }
  return value
}

const getRankingClass = (index) => {
  if (index < 3) return `top-${index + 1}`
  return ''
}

// 获取分类名称
const getCategoryName = (categoryId) => {
  const category = categories.value.find(c => c.id === categoryId)
  return category ? category.name : '未知'
}

// 获取标签类型
const getTagType = (categoryId) => {
  const types = ['', 'success', 'info', 'warning', 'danger']
  const index = categories.value.findIndex(c => c.id === categoryId) % 5
  return types[index]
}
</script>

<style scoped>
.platform-card {
  border-radius: 8px;
  transition: transform 0.3s;
}

.platform-card:hover {
  transform: translateY(-5px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.platform-name {
  font-size: 18px;
  font-weight: bold;
}

.hot-search-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.hot-search-item {
  display: flex;
  align-items: center;
  padding: 8px 0;
  cursor: pointer;
  border-radius: 4px;
  transition: background-color 0.2s;
  position: relative;
}

.hot-search-item:hover {
  background-color: #f5f7fa;
}

.ranking {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  background-color: #f5f7fa;
  color: #909399;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  font-size: 14px;
  font-weight: bold;
  flex-shrink: 0;
}

.top-1 {
  background-color: #f56c6c;
  color: white;
}

.top-2 {
  background-color: #e6a23c;
  color: white;
}

.top-3 {
  background-color: #67c23a;
  color: white;
}

.content {
  flex: 1;
  min-width: 0;
}

.title {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 4px;
}

.hot-value {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #909399;
}

.icon {
  margin-right: 4px;
  fill: #f56c6c;
}

.category-tag {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 2;
}
</style>